// Vanity overriding wisdom.

#include <bits/stdc++.h>

using ll = long long;

namespace FastIO {
	template <typename T> inline T read() { T x = 0, w = 0; char ch = getchar(); while (ch < '0' || ch > '9') w |= (ch == '-'), ch = getchar(); while ('0' <= ch && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return w ? -x : x; }
	template <typename T> inline void write(T x) { if (x == 0) return; write<T>(x / 10), putchar(x % 10 + '0'); }
	template <typename T> inline void print(T x) { if (x < 0) putchar('-'), x = -x; if (x == 0) putchar('0'); write<T>(x); }
	template <typename T> inline void print(T x, char ed) { print<T>(x), putchar(ed); }
}; using namespace FastIO;

const int V = 1000000;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
inline void chkmax(ll &x, ll y) { x = std::max(x, y); }

#define MAXN 200001
int a[MAXN];
ll c[V + 1];

void solve() {
	int n = read<int>(); ll tg = 0, mx = 0;
	for (int i = 1; i <= n; ++i) a[i] = read<int>();
	for (int i = 1; i <= V; ++i) c[i] = -INF;
	for (int i = 2; i <= n; ++i) {
		if (a[i] == a[i - 1]) tg += a[i], mx += a[i];
		else chkmax(c[a[i - 1]], std::max(mx - tg, c[a[i]] + a[i]));
		mx = std::max(mx, c[a[i - 1]] + tg);
	} print<ll>(mx, '\n');
}

// Let f[i][j] be, after considering the coloration of 1...i, the last colored with the opposite color being j, and the maximized answer
// two kinds of transition are as follows:
// f[i - 1][j] + (a[i] == a[i - 1] ? a[i] : 0) -> f[i][j] (1)
// f[i - 1][j] + (a[i] == a[j] ? a[i] : 0) -> f[i][i - 1] (2)
// shrink the dp array so that it becomes 1 dimentional
// let c[v] be max_{a[j] = v} (dp[j])
// therefore, f[i][i - 1] <- max(max dp[j], c[a[i]] + a[i]), after which we update c[a[i - 1]]
// for transition (1), we just use an all time tag

int main() {
	freopen("color.in", "r", stdin);
	freopen("color.out", "w", stdout);
	int T = read<int>(); while (T--) solve(); return 0;
}
